//
//  ActivityListViewController.m
//  SXH
//
//  Created by Ryan on 15/12/10.
//  Copyright © 2015年 lyss. All rights reserved.
//

#import "ActivityListViewController.h"
#import "ActivityListCell.h"
#import "CommonService.h"
#import "ActivityHeadInfoModel.h"
#import "MJRefresh.h"
#import "ActivityDetailViewController.h"

@interface ActivityListViewController ()<UITableViewDelegate,UITableViewDataSource>
{
    MBProgressHUD *HUD;
    MJRefreshHeaderView *_header;
    MJRefreshFooterView *_footer;
    int page;
}
@end

@implementation ActivityListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = GrayColor;
    page = 1;
    [self initMainView];

}

- (void)initMainView
{
    if (self.tableView == nil) {
        self.tableView = [[UITableView alloc]init];
        [self.view addSubview:self.tableView];
        [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self.view).with.insets(UIEdgeInsetsMake(62, 0, 0, 0));
        }];
        [self.tableView setTableHeaderView:[[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 5)]];
        [self.tableView setTableFooterView:[[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 5)]];
        self.tableView.delegate = self;
        self.tableView.dataSource = self;
        self.tableView.backgroundColor = [UIColor clearColor];
        self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    [self addHeader];
    [self addFooter];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 206/320.f*SCREEN_WIDTH;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.listArray count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *cellId = @"ActivityListCell";
    ActivityListCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if (cell == nil)
    {
        cell = [[[NSBundle mainBundle]loadNibNamed:cellId owner:self options:nil]lastObject];
    }
    cell.model = [self.listArray objectAtIndex:indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ActivityHeadInfoModel *model = [self.listArray objectAtIndex:indexPath.row];
    ActivityDetailViewController *mvc = [[ActivityDetailViewController alloc]init];
    mvc.spotId = model.id;
    [self.navigationController pushViewController:mvc animated:YES];
}


- (IBAction)doback:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

//http://api.leyouss.com/Photo_contest/queryContest?limit=5&page=1
- (void) getActivityListData:(int)pageNo  RefreshView:(MJRefreshBaseView *)refreshView
{
    CommonService *service = [[CommonService alloc] init];
    NSString *strPageNo = [NSString stringWithFormat:@"%d", pageNo];
    NSDictionary *parm = @{@"limit":@"20",
                           @"page":strPageNo
                           };
    if (self.parkid.length > 0) {
        parm = @{@"limit":@"20",
                 @"page":strPageNo,
                 @"park_id":self.parkid
                 };
    }
    [service getNetWorkData:parm
                       Path:ActivityList_PATH
                  Successed:^(id entity) {
                      NSDictionary *dic = (NSDictionary *)entity;
                      
                      if (pageNo == 1)
                      {
                        [self.listArray removeAllObjects];
                        self.listArray = [ActivityHeadInfoModel arrayOfModelsFromDictionaries:[dic objectForKey:@"data"]];
                      }
                      else
                      {
                        [self.listArray addObjectsFromArray:[ActivityHeadInfoModel arrayOfModelsFromDictionaries:[dic objectForKey:@"data"]]];
                      }

                      [self doneWithView:refreshView];
                  } Failed:^(NSInteger errorCode, NSString *message) {

                      [refreshView endRefreshing];
                  }];
}

- (void)addFooter
{
    //__unsafe_unretained OAAnnoViewController *vc = self;
    MJRefreshFooterView *footer = [MJRefreshFooterView footer];
    footer.scrollView = self.tableView;
    footer.beginRefreshingBlock = ^(MJRefreshBaseView *refreshView) {
        
        ++page;
        [self getActivityListData:page RefreshView:refreshView];
        
        NSLog(@"%@----开始进入刷新状态", refreshView.class);
    };
    _footer = footer;
}

- (void)addHeader
{
    //__unsafe_unretained OAAnnoViewController *vc = self;
    MJRefreshHeaderView *header = [MJRefreshHeaderView header];
    header.scrollView = self.tableView;
    
    header.beginRefreshingBlock = ^(MJRefreshBaseView *refreshView) {
        // 进入刷新状态就会回调这个Block
        [self getActivityListData:1 RefreshView:refreshView];
        NSLog(@"%@----开始进入刷新状态", refreshView.class);
    };
    header.endStateChangeBlock = ^(MJRefreshBaseView *refreshView) {
        // 刷新完毕就会回调这个Block
        NSLog(@"%@----刷新完毕", refreshView.class);
    };
    header.refreshStateChangeBlock = ^(MJRefreshBaseView *refreshView, MJRefreshState state) {
        // 控件的刷新状态切换了就会调用这个block
        switch (state) {
            case MJRefreshStateNormal:
                NSLog(@"%@----切换到：普通状态", refreshView.class);
                break;
            case MJRefreshStatePulling:
                NSLog(@"%@----切换到：松开即可刷新的状态", refreshView.class);
                break;
            case MJRefreshStateRefreshing:
                NSLog(@"%@----切换到：正在刷新状态", refreshView.class);
                break;
            default:
                break;
        }
    };
    [header beginRefreshing];
    _header = header;
}

- (void)doneWithView:(MJRefreshBaseView *)refreshView
{
    // 刷新表格
    [self.tableView reloadData];
    // (最好在刷新表格后调用)调用endRefreshing可以结束刷新状态
    [refreshView endRefreshing];
}


@end
